
const tool = require('../tool');
const { handleReplace } = require('../tool');


module.exports = nav_left_menu_generate;

async function nav_left_menu_generate(projectConfig) {

  //菜单生成校验
  var isGenerate = projectConfig.is_generate_nav_left;
  if(isGenerate == 'false') {
    console.log("不生成左侧导航菜单");
    return;
  }

  /* 
    菜单请求的修改
  
  */

  const menuDataStr = handleMakeNavMenuData(projectConfig);
  //引入
  var menuLeftTpl = require('../template/nav_left_tpl');
  menuLeftTpl = tool.handleReplace(menuLeftTpl, {
    navLeftDataList: menuDataStr
  });
  // console.log(menuLeftTpl);

  console.log(666, projectConfig)

  //写入
  tool.writeFile({
    outputPath: '../src/components/nav_left.vue',
    fileContent: menuLeftTpl
  })
}

//根据数据生成菜单
function handleMakeNavMenuData(projectConfig) {
  var nav_menu_data = `[ `;
  var i = 0;
  for (var item in projectConfig) {
    var curItem = projectConfig[item];
    //根据配置里的nav_name说明字段生成菜单和菜单文件
    if (curItem.nav_name) {
      i++;
      var sub_menu_data = `
        {
          menuName: '${curItem.nav_name}',
          menuUrl: "",
          menu_href: '${i}',
          icon: "el-icon-s-unfold",
          `;
      var sysMenuListStr = `sysMenuList: [ `;
      var subIndex = 0;
      for (var subItem in curItem) {
        var curSubItem = curItem[subItem];
        if (curSubItem.nav_name) {
          subIndex++;
          //当前路由名 有路由的重名子页面需要配置专属名
          var curRouterName = subItem;
          if (subItem.includes('table') && curItem[subItem].table_name) {
            curRouterName = curItem[subItem].table_name; //具有多个子列表的模块可以实现多列表配置
          }
          if(subItem.includes('bg_editor_page')) {
            curRouterName = curItem[subItem].bg_editor_page_name; //富文本编辑器页面
          }
          if(curItem[subItem].is_show_nav == 'false') {
            continue;
          }
          var str = `
            {
              menuName: '${curSubItem.nav_name}',
              menuUrl: '/${item}_${curRouterName}',
              menu_href: ${ i + '-' + subIndex},
            },`
          sysMenuListStr += str;
        }
      }
      sysMenuListStr += `
          ]`;
      sub_menu_data += sysMenuListStr + `
        },
      `;
      nav_menu_data += sub_menu_data;
    }

  }
  nav_menu_data += `
  ]`;
  return nav_menu_data;
}